<?php
/**
 * Created by PhpStorm.
 * 广告调用标签,是插件广告管理 yoby_ad模块的使用
 * User: yoby
 * Date: 2018/7/1
 * Time: 18:38
 */
 if(!defined('DEDEINC')) exit('Request Error!');

function lib_ad( &$ctag, &$refObj )
{
    global $dsql,$envs;
    $attlist="typeid|0,row|12,titlelen|24,orderby|sortrank,orderway|desc";
    FillAttsDefault($ctag->CAttribute->Items,$attlist);
   extract($ctag->CAttribute->Items,EXTR_SKIP);
    $revalue ="";
$innertext = $ctag->GetInnerText();
$ctp = new DedeTagParse();
$ctp->SetNameSpace('field','[',']');

$where = ($typeid==0) ?" and 1=1 " :"  and tid=".$typeid;
if($orderby!='') $orderby = " ORDER BY $orderby $orderway ";
$sql = "SELECT * FROM #@__yoby_adlist WHERE isshow=1 $where $orderby limit 0,$row";
$dsql->Execute('me',$sql);

    $GLOBALS['autoindex'] = 0;

while($rs = $dsql->GetArray('me'))
{        $GLOBALS['autoindex']++;
    $rs['title'] = cn_substr($rs['title'],$titlelen);
$ctp->LoadSource($innertext);

foreach($ctp->CTags as $tagid=>$ctag) {
        if(!empty($rs[strtolower($ctag->GetName())])) {
            $ctp->Assign($tagid,$rs[$ctag->GetName()]);
            }
}
    $revalue .= $ctp->GetResult();}
            return $revalue;

}